/*
 * LCG - Linear Congruential Generator (pseudorandom numbers)
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package lib3f;

// This is a very simple pseudorandom number generator.
// It is designed to work with numbers across all platforms, but has only been tested
// with Neko and Flash.
// It is fast, but neither secure nor the best available option for randomness.
// See the Wikipedia article on Linear Congruential Generator for more details.

class LCG
{

	public var seed : Int;

	public function new (?start : Int)
	{
		if (start==null)
			seed = Std.int(Math.random()*1046527);
		else
			seed = start;
	}

	public function randInt(maxsize : Int) : Int
	{
		// random int value between 0 and maxsize
		var tmp : Int = (seed * 883) + 883;
		tmp = tmp % 1046527;
		seed = Std.int(tmp);
		if (seed<=0) seed=1;
		return tmp %(maxsize+1);		
	}
			
}
